<template>
  <el-card class="box-card" style="margin-top:-10px;" v-loading="loading1">
    <div class="tac" style="height:30px;margin-top:-10px;color: #909399;font-weight: 700;">区域列表</div>
    <el-table :data="list" border style="width: 100%;">
      <el-table-column align="center" prop="cityId" label="区域ID"></el-table-column>
      <el-table-column align="center" prop="name" label="城市名称"></el-table-column>
      <el-table-column align="center" prop="cityName" label="区域名称"></el-table-column>
      <el-table-column width="220px" align="center" label="操作" v-loading="loading">
        <template slot-scope="scope">
          <el-button @click="removcity(scope)" type="danger" icon="el-icon-delete" circle></el-button>
          <el-button
            @click="changedata(scope.row)"
            type="primary"
            icon="el-icon-edit"
            circle
            style=" margin-left:30px;"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin-top:10px;" class="tac">
      <el-pagination
        :hide-on-single-page="true"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      total: 0,
      loading: false,
      loading1: true,

      currentPage1: 1
    };
  },
  methods: {
    changedata(data) {
      // this.$router.push({ path: "/area/add", query: { data: data } });
      this.$router.push( {name:'区域编辑',params:data});
    },
    removcity(scope) {
      this.loading = true;
      let $index = scope.$index;

      this.$confirm("您确定要删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let data = {
            areaId: scope.row.areaId
          };
          let url = "/area/delById";
          this.$axios
            .post(url, data)
            .then(res => {
              this.$message.success("删除城市信息成功");
              this.list.splice($index, 1);
            })
            .catch(err => {
              this.$message.error("删除错误");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    formatter(cellValue) {
      return cellValue ? "是" : "否";
    },

    handleSizeChange(num) {
      this.getList(num);
    },

    handleCurrentChange(num) {
      this.getList(num);
    },

    getList(pageNum) {
      this.loading = true;
      let url = "/area/getList?pageNum=" + pageNum;
      this.$axios
        .get(url)
        .then(res => {
          this.list = res.areas;
          this.total = res.total;
          this.loading1 = false;
        })
        .catch(error => {
          this.$message.error("数据获取错误");
          this.loading1 = false;
        });
    }
  },
  created() {
    this.getList();
  }
};
</script>
<style lang="less" scoped>
.el-table td,
.el-table th {
  padding: 5px 0px;
}
</style>